import 'package:flutter/material.dart';

enum BoxToReferencePosition {
  /// 在参考物的上方
  top,

  /// 在参考物的中心
  center,

  /// 在参考物的下方
  bottom
}

class KqOverFlowBox extends StatefulWidget {
  /// 是否显示
  final bool? visible;

  /// 参考物
  final Widget referenceWidget;

  /// overflowbox
  final Widget overflowBoxWidget;

  /// overflowBox最大宽度
  final double maxWidth;

  /// overflowBox最大宽度
  final double maxHeight;

  /// 位置 默认：boxToReferencePositionTop
  final BoxToReferencePosition? position;

  /// 相对参考物位置的Alignment 默认:Alignment.topRight
  final Alignment? alignment;

  /// margin
  final EdgeInsetsGeometry? margin;

  const KqOverFlowBox(
      {this.visible = false,
      required this.referenceWidget,
      required this.overflowBoxWidget,
      required this.maxWidth,
      required this.maxHeight,
      this.position = BoxToReferencePosition.top,
      this.alignment = Alignment.topRight,
      this.margin = EdgeInsets.zero,
      super.key});

  @override
  State<KqOverFlowBox> createState() => _KqOverFlowBoxState();
}

class _KqOverFlowBoxState extends State<KqOverFlowBox> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: widget.position == BoxToReferencePosition.center
          ? Alignment.center
          : widget.position == BoxToReferencePosition.top
              ? Alignment.topRight
              : Alignment.bottomRight,
      children: [
        widget.referenceWidget,
        Visibility(
            visible: widget.visible ?? false,
            child: Container(
              margin: widget.margin,
              height: 1,
              child: OverflowBox(
                alignment: widget.alignment ?? Alignment.bottomRight,
                maxWidth: widget.maxWidth,
                maxHeight: widget.maxHeight,
                child: widget.overflowBoxWidget,
              ),
            ))
      ],
    );
  }
}
